package com.yjz.demo.banner

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.Switch
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
import com.yjz.demo.R
import com.yjz.ui.banner.BannerLayout
import com.yjz.ui.banner.core.BannerModel
import com.yjz.ui.banner.indicator.BannerIndicator
import com.yjz.ui.banner.indicator.CircleIndicator
import com.yjz.ui.banner.indicator.NumberIndicator
import java.util.*

class BannerDemoActivity : AppCompatActivity() {
    private var urls = arrayOf(
        "https://www.devio.org/img/beauty_camera/beauty_camera1.jpg",
        "https://www.devio.org/img/beauty_camera/beauty_camera3.jpg",
        "https://www.devio.org/img/beauty_camera/beauty_camera4.jpg",
        "https://www.devio.org/img/beauty_camera/beauty_camera5.jpg",
        "https://www.devio.org/img/beauty_camera/beauty_camera2.jpg",
        "https://www.devio.org/img/beauty_camera/beauty_camera6.jpg",
        "https://www.devio.org/img/beauty_camera/beauty_camera7.jpg",
        "https://www.devio.org/img/beauty_camera/beauty_camera8.jpeg"
    )
    private var indicator: BannerIndicator<*>? = null
    private var autoPlay: Boolean = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_banner)
        initView(CircleIndicator(this), false)
        findViewById<Switch>(R.id.auto_play).setOnCheckedChangeListener { _, isChecked ->
            autoPlay = isChecked
            initView(indicator, autoPlay)
        }
        findViewById<View>(R.id.tv_switch).setOnClickListener {
            if (indicator is BannerIndicator) {
                initView(NumberIndicator(this), autoPlay)
            } else {
                initView(CircleIndicator(this), autoPlay)
            }
        }
    }

    private fun initView(hiIndicator: BannerIndicator<*>?, autoPlay: Boolean) {
        this.indicator = hiIndicator
        val bannerLayout = findViewById<BannerLayout>(R.id.banner)
        val moList: MutableList<BannerModel> = ArrayList()
        for (i in 0..5) {
            val mo = BannerModel(urls[i % urls.size])
            moList.add(mo)
        }
        bannerLayout!!.setBannerIndicator(hiIndicator)
        bannerLayout.setAutoPlay(autoPlay)
        bannerLayout.setIntervalTime(2000)
        bannerLayout.setOnBannerClickListener { _, bannerMo, position ->
            Toast.makeText(this@BannerDemoActivity, position.toString() + "url:" + bannerMo.url, Toast.LENGTH_SHORT).show()
        }
        //自定义布局
        bannerLayout.setBannerData(R.layout.banner_item_layout, moList)
        bannerLayout.setBindAdapter { viewHolder, mo, position ->
            val imageView: ImageView = viewHolder.findViewById(R.id.iv_image)
            Glide.with(this@BannerDemoActivity).load(mo.url).into(imageView)
            val titleView: TextView = viewHolder.findViewById(R.id.tv_title)
            titleView.text = mo.url
        }
    }
}
